/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sniff;

import java.io.*;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
 *
 * @author trungdung
 */
  public class Counter extends Thread {
        @Override
        public void run() {
            Random generator=new Random();
            while(true) {           
                StaffCheck sc=new StaffCheck();
                sc.setVisible(true);
                try {
                    sleep(60000);
                    sc.closeTimeout();
                } catch(InterruptedException e) {
                    e.printStackTrace();
                }
                int nextMillis=(300+generator.nextInt(300))*1000;
                System.out.println(nextMillis);
                try {
                    sleep(nextMillis);
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                }
            }
        }    
    

class StaffCheck extends javax.swing.JFrame {

    /**
     * Creates new form StaffCheck
     */
    private String start,end;
    private int mstart,mend;
    public StaffCheck() {
        initComponents();
        Format  formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar cal = Calendar.getInstance();
        mstart=new Long(cal.getTimeInMillis()/1000).intValue();
        start=formatter.format(cal.getTime());
    }
    
    public void closeTimeout() {
        Format  formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Format formatter2 = new SimpleDateFormat("yyyy-MM-dd");        
        Calendar cal = Calendar.getInstance();
       // mend=new Long(cal.getTimeInMillis()/1000).intValue();
       // end=formatter.format(cal.getTime());   
       //int playtime = (new Long((mend-mstart)/1000)).intValue();
       // int hour=(new Long(playtime/3600)).intValue();
        //int minute=(new Long((playtime-hour*3600)/60)).intValue();
        //int second=(new Long(playtime-hour*3600-minute*60)).intValue();
        //System.out.println(hour+":"+minute+":"+second);
        try {
            FileWriter logfw=new FileWriter("log-"+formatter2.format(cal.getTime())+".txt",true);
            BufferedWriter out = new BufferedWriter(logfw);
            out.write(mstart+";"+start+";"+"0\n");
            //Close the output stream
            out.close();            
        } catch (IOException ex) {
            ex.printStackTrace();
        }        
        dispose();
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButtonCheck = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("Xác nhận");

        jButtonCheck.setText("OK");
        jButtonCheck.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCheckActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(66, 66, 66)
                .addComponent(jButtonCheck, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(74, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(57, Short.MAX_VALUE)
                .addComponent(jButtonCheck, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(31, 31, 31))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonCheckActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCheckActionPerformed
        // TODO add your handling code here:
        Format  formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Format formatter2 = new SimpleDateFormat("yyyy-MM-dd");        
        Calendar cal = Calendar.getInstance();
       // mend=cal.getTimeInMillis();
       // end=formatter.format(cal.getTime());   
       // int playtime = (new Long((mend-mstart)/1000)).intValue();
       // int hour=(new Long(playtime/3600)).intValue();
       // int minute=(new Long((playtime-hour*3600)/60)).intValue();
       // int second=(new Long(playtime-hour*3600-minute*60)).intValue();
       // System.out.println(hour+":"+minute+":"+second);
        try {
            FileWriter logfw=new FileWriter("log-"+formatter2.format(cal.getTime())+".txt",true);
            BufferedWriter out = new BufferedWriter(logfw);
            out.write(mstart+";"+start+";"+"1\n");
            //Close the output stream
            out.close();            
        } catch (IOException ex) {
            ex.printStackTrace();
        }        
        dispose();
    }//GEN-LAST:event_jButtonCheckActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonCheck;
    // End of variables declaration//GEN-END:variables
}

}